<!DOCTYPE html>
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%
    String path = request.getContextPath();
%>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.5/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.5/themes/color.css">
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.5/demo/demo.css">
    <script type="text/javascript" src="<%=path%>/js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="<%=path%>/js/jquery-easyui-1.5/jquery.easyui.min.js"></script>
</head>
<body>
<h2>用户管理</h2>

<table id="dg" title="用户管理" class="easyui-datagrid" style="width:700px;height:250px"
       url="/user/getUsers.do"
       toolbar="#toolbar" pagination="true"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="userId" width="50">user Id</th>
        <th field="nickName" width="50">nick Name</th>
        <th field="realName" width="50">real Name</th>
        <th field="phoneNumber" width="50">phone number</th>
        <th field="orderField" width="50">order field</th>
        <th field="updatedDate" width="50">update Time</th>
        <th field="remark" width="50">remark</th>
    </tr>
    </thead>
</table>
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加新用户</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true"
       onclick="editUser()">修改用户信息</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除用户</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
        <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">用户信息</div>
        <div style="margin-bottom:10px">
            <input name="nickName" class="easyui-textbox" required="true" label="昵称:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input name="realName" class="easyui-textbox" label="真实姓名:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input name="phoneNumber" class="easyui-textbox" label="手机号:" validType="phoneNum" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input name="remark" class="easyui-textbox" label="备注:" style="width:100%">
        </div>
        <%-- <div style="margin-bottom:10px">
             <input name="email" class="easyui-textbox" required="true" validType="email" label="Email:"
                    style="width:100%">
         </div>--%>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
</div>
<script type="text/javascript">
    $.extend($.fn.validatebox.defaults.rules, {
        phoneNum: { //验证手机号
            validator: function (value, param) {
                return /^1[3-8]+\d{9}$/.test(value);
            },
            message: '请输入正确的手机号码。'
        },

        telNum: { //既验证手机号，又验证座机号
            validator: function (value, param) {
                return /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\d3)|(\d{3}\-))?(1[358]\d{9})$)/.test(value);
            },
            message: '请输入正确的电话号码。'
        }
    });

    var url;
    function newUser() {
        $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新用户');
        $('#fm').form('clear');
        url = "<%=path%>/user/addUser.do";
    }
    function editUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改用户信息');
            $('#fm').form('load', row);
            url = '<%=path%>/user/updateUser.do?userId=' + row.userId;
        }
    }
    function saveUser() {
        var nickName = $('#nickName').val();
        var realName = $('#realName').val();
        var phoneNumber = $('#phoneNumber').val();
        var remark = $('#remark').val();
        alert(nickName);
        $('#fm').form('submit', {
            url: url,
//            data: 'nickName=' + nickName + "&realName" + realName + "&phoneNumber=" + phoneNumber + "&remark" + remark,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.errorMsg) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                } else {
                    $('#dlg').dialog('close');        // close the dialog
                    $('#dg').datagrid('reload');    // reload the user data
                }
            }
        });
    }
    function destroyUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', 'Are you sure you want to destroy this user?', function (r) {
                if (r) {
                    $.post('<%=path%>/user/destroyUser.do', {id: row.userId}, function (result) {
                        if (result.success) {
                            $('#dg').datagrid('reload');    // reload the user data
                        } else {
                            $.messager.show({    // show error message
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        }
                    }, 'json');
                }
            });
        }
    }
</script>
</body>
</html>